import { EResType } from "../../../Script/Common/Enum";
import { ILotteryGrade } from "../../../Script/Common/GlobalData";
import { IFlyResData } from "../../../Script/Interface/IMessage";
import { SceneGame } from "../../../Script/SceneGame";
import GameUtil from "../../../Script/Util/GameUtil";
import { LoadUtil } from "../../../Script/Util/LoadUtil";
import Eraser from "./Eraser";

interface IRewardData {
    /** 中奖数值 */
    id: number;
    /** 奖励金额 */
    value: number;
    /** 奖励索引 */
    pos?: number;
}

const { ccclass, property } = cc._decorator;

@ccclass
export default class Gold extends Eraser {
    
    private rewards: IRewardData[] = [];

    public restart() {
        super.restart();

        this.rewards = [];

        const p = Math.random() * 100;
        if (p <= this.data.p) { // 中奖
            // 小额最多中奖4次
            const num = Math.ceil(Math.random() * this.grade.max);
            const arr = this.getIndexArr();
            const indexs = GameUtil.getRandomElement(arr, num);
            this.refreshItems(indexs, this.grade);

            console.log("中奖--", this.grade.id, '---', num);
        } else {
            this.refreshItems([], this.grade);

            console.log("未中奖~");
        }
    }

    private refreshItems(indexs: number[], grade: ILotteryGrade) {
        const arr = this.exclude();
        this.items.children.forEach((o, i) => {
            this.positions.push(cc.v2(o.x, o.y));
            const id = this.random(arr);
            const value = this.random(this.totalArr);
            this.refreshItem(o, { id, value });
        });
        this.positionProgress = new Array(this.positions.length);

        // 重刷中奖展示
        for (let k = 0; k < indexs.length; k++) {
            const index = indexs[k];
            const node = this.items.children[index];
            const value = this.random(grade.values);
            const reward = { id: Math.floor(Math.random() * 3), value: value, pos: index };
            this.refreshItem(node, reward);
            this.rewards.push(reward);
        }
    }

    /** 兑奖 */
    protected async checkReward() {
        try {
            super.checkReward();
            const len = this.rewards.length;
            if (len > 0) {
                for (let i = 0; i < len; i++) {
                    const reward = this.rewards[i];
                    const index = reward.pos;
                    const data = SceneGame.instance.getFlyResData();
                    const flyData: IFlyResData = {
                        resType: EResType.Money,
                        parent: data.flyNode,
                        from: this.items.children[index],
                        to: data.money,
                        value: reward.value * (reward.id + 1),  // 金、鍂、鑫分别是123倍奖励
                        flyTimes: 10,
                        maxWidth: 60,
                        maxHeight: 60,
                    }
                    GameUtil.instance.flyResIcon(flyData);
                    console.log(`动物id:${reward.id}  中奖金额:${reward.value}  位置: ${index}`);
                    await GameUtil.sleep(200);
                }
            } else {
                console.log("未中奖~");
            }
        } catch (error) {
            console.log(error);
        }
    }

    private getIndexArr() {
        const temp: number[] = [];
        const len = this.items.children.length;
        for (let i = 0; i < len; i++) {
            temp.push(i);
        }
        return temp;
    }

    private exclude() {
        const ex = [];
        for (let i = 0; i < 6; i++) {
            if (i > 2) {
                ex.push(i);
            }
        }
        return ex;
    }

    private refreshItem(node: cc.Node, data: IRewardData) {
        const icon = cc.find("sprite", node).getComponent(cc.Sprite);
        const num = cc.find("layout/num", node).getComponent(cc.Label);
        num.string = `${GameUtil.changeUnit(data.value, 0)}`;
        LoadUtil.instance.setSpriteFrame(icon, EResType.LotteryGold, data.id);
    }
}